#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<vector<int>> threeSum(vector<int>& nums) {
    vector <vector<int>> res;
    sort(nums.begin(),nums.end());

    if(nums[0]>0)
        return res;
    for(int i=1;i<nums.size()-1;i++)
    {
        int left=0;
        int right=nums.size()-1;
        int temp1=i;
        int temp2=i;
        int temp3 = i;
        if(nums[i]==nums[i+1]&&i<nums.size()-2)
            continue; 

        while(nums[temp3]==nums[temp3-1]&&temp3>2){
            temp2--;
            temp3--;
        }temp2--;
        while(left<right&&left<temp1&&right>temp2)
        {
            if(nums[left]+nums[i]+nums[right]<0)
            {
                left++;

            }
            else if(nums[left]+nums[i]+nums[right]>0)
            {
                right--;
                if(right==i)
                    right--;
            }
            else
            {
                res.insert(res.begin(),{nums[left],nums[i],nums[right]});
                right--;
                while(nums[right]==nums[right+1])right--;
                
            }
        }    
    }

    return res;        
}

int main()
{
    vector<vector<int>> res;
    vector<int> nums = {-4,-2,-2,-2,0,1,2,2,2,3,3,4,4,6,6};
    res=threeSum(nums);
    // 打印结果
    for (const auto& triplet : res) {
        for (int num : triplet) {
            std::cout << num << ' ';
        }
        std::cout << '\n'; // 换行，以区分不同的三元组
    }
    return 0;
}